package com.opd.soeghtool;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Map;

public class GameListPopupWindow extends PopupWindow implements View.OnTouchListener {
    Context context;
    View popupView;
    ListView lv_gamelist;
    MyAdapter myAdapter;
    ArrayList<Map<String, Object>> gameList;
    Button btn_pop_gamelist_back;
    GlobalVar mGlobalVar;
    int mType, mPosition;
    public GameListPopupWindow(Context context, ArrayList<Map<String, Object>> gameList, GlobalVar mGlobalVar, int type, int pos)
    {
        super(context);
        this.context = context;
        this.gameList = gameList;
        this.mGlobalVar = mGlobalVar;
        this.mType = type;
        this.mPosition = pos;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        popupView = inflater.inflate(R.layout.pop_gamelist_layout, null);
        lv_gamelist = (ListView)popupView.findViewById(R.id.lv_pop_gamelist);
        this.setContentView(popupView);
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.MATCH_PARENT);//屏幕的高
        // 设置SelectPicPopupWindow弹出窗体可点击
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setFocusable(true);
        myAdapter = new MyAdapter(context);
        lv_gamelist.setAdapter(myAdapter);
        btn_pop_gamelist_back = (Button)popupView.findViewById(R.id.btn_pop_gamelist_back);
        btn_pop_gamelist_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }

    class MyAdapter extends BaseAdapter {
        private Context context = null;

        public MyAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return gameList.size();
        }

        @Override
        public Object getItem(int position) {
            return gameList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolderPop mHolder;
            final int pos = position;
            if (convertView == null) {
                mHolder = new MyAdapter.ViewHolderPop();
                LayoutInflater inflater = LayoutInflater.from(context);
                //convertView = inflater.inflate(R.layout.listview_layout, null, true);
                convertView = inflater.inflate(R.layout.listview_rellayout, null, true);
                //mHolder.tv_lv_id = (TextView) convertView.findViewById(R.id.tv_lv_id);
                mHolder.iv_lv_gameicn = (ImageView) convertView.findViewById(R.id.iv_lv_gameicn);
                mHolder.tv_lv_gamename = (TextView) convertView.findViewById(R.id.tv_lv_gamename);
                mHolder.btn_lv_launch = (Button) convertView.findViewById(R.id.btn_lv_launch);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ViewHolderPop) convertView.getTag();
            }

            //mHolder.tv_lv_id.setText((String)gameList.get(position).get("id"));
            mHolder.tv_lv_gamename.setText((String)gameList.get(position).get("name"));
            mHolder.iv_lv_gameicn.setImageDrawable((Drawable) gameList.get(position).get("icn"));
            mHolder.btn_lv_launch.setText((String) gameList.get(position).get("start"));

            mHolder.btn_lv_launch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //startLocalApp((String)dataList.get(pos).get("pckname"));
                    Log.d("SOEPOP",(String) gameList.get(pos).get("pckname"));
                    mGlobalVar.setIsAddToList(true);
                    /*
                    for(int a=0;a<mGlobalVar.getGameGpTotalNum();a++)
                    {
                        if(((String) gameList.get(pos).get("pckname")).contains(mGlobalVar.getPackageName(a)))
                        {
                            dismiss();
                            return;//mGlobalVar.setPackageName(mPosition,(String) gameList.get(pos).get("pckname"));
                        }
                    }
                    */
                    if(mType==Transfer_Con.LIST_TYPE_USER_LOST)
                    {
                        mGlobalVar.setPackageName(mPosition,(String) gameList.get(pos).get("pckname"));
                    }
                    else {
                        if (mGlobalVar.getIsLastConDeviceKB())
                        {
                            mGlobalVar.setPackageNameKB(mGlobalVar.getGameGpTotalNumKB(), (String) gameList.get(pos).get("pckname"));
                            mGlobalVar.setGameGpTotalNumKB(mGlobalVar.getGameGpTotalNumKB() + 1);
                            mGlobalVar.setGameListTotalNumKB(mGlobalVar.getGameListTotalNumKB() + 1);
                        }
                        else {
                            mGlobalVar.setPackageName(mGlobalVar.getGameGpTotalNum(), (String) gameList.get(pos).get("pckname"));
                            mGlobalVar.setGameGpTotalNum(mGlobalVar.getGameGpTotalNum() + 1);
                            mGlobalVar.setGameListTotalNum(mGlobalVar.getGameListTotalNum() + 1);
                        }
                    }
                    dismiss();
                }
            });
            return convertView;
        }

        class ViewHolderPop {
            //private TextView tv_lv_id;
            private ImageView iv_lv_gameicn;
            private TextView tv_lv_gamename;
            private Button btn_lv_launch;
        }
    }
}
